/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spoxcy.business;


import com.spoxcy.po.LoginPO;
import com.spoxcy.util.DBTester;
import com.spoxcy.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Denis.Duan
 * @date Apr 20, 2009
 */
public class UserValidationServiceImpl implements IUserValidationService{

    private DBUtil dbUtility;

    private final String VALIDATION_SQL = "select * from t_user_info where " +
            "user_name=? and pword=?";

    private final String AJAX_CHECK_SQL = "select * from t_user_info where " +
            "user_name=?";

    public UserValidationServiceImpl(DBUtil dbUtility) {
        this.dbUtility = dbUtility;
    }

    /**
     * The method is used for user login purpose, which to check out whether
     * the username and password the usr input is correct
     *
     * @param login
     * @return
     */
    public boolean isExistingUser(LoginPO login){
        boolean existingFlag = false;

        Connection connection = null;

         try {
            connection = this.dbUtility.getConnection();
            PreparedStatement statement = connection.prepareStatement(VALIDATION_SQL);
            statement.setString(1, login.getUsername());
            statement.setString(2, login.getPassword());

            ResultSet rs = statement.executeQuery();

            //if the next() returns true, means there is a matching found
            if(rs.next()){
                existingFlag = true;
            }

            connection.close();

         } catch (SQLException ex) {
            Logger.getLogger(RegistrationServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(RegistrationServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return existingFlag;
    }


    /**
     * The mathod is used for the Ajax function in the register.html to check
     * out the availability of username
     *
     * @param username
     * @return
     */
    public boolean isUsernameAvailable(String username){
        boolean available = false;

         Connection connection = null;

         try {
            connection = this.dbUtility.getConnection();
            PreparedStatement statement = connection.prepareStatement(AJAX_CHECK_SQL);
            statement.setString(1, username);

            ResultSet rs = statement.executeQuery();

            //if the next() returns true, means there is a matching found
            if(rs.next()){
                available = true;
            }

            connection.close();

         } catch (SQLException ex) {
            Logger.getLogger(RegistrationServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(RegistrationServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return available;
    }


    //Tester
    public static void main(String[] args) {
        DBTester tester = new DBTester();
        UserValidationServiceImpl validator = new UserValidationServiceImpl(tester);
        LoginPO po = new LoginPO();
        po.setPassword("sdfa");
        po.setUsername("sdfa");

        System.out.println(validator.isUsernameAvailable("man"));
        //boolean flag = validator.isExistingUser(po);
        //System.out.println(flag);
    }

}
